package cn.edu.zut.dao;

import cn.edu.zut.model.ReaderType;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository()
public class ReaderTypeDaoImpl implements ReaderTypeDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    ReaderType readerType;
    @Autowired
    List<ReaderType> readerTypeList;

//    插入读者类型的信息
    public int insertReaderType(ReaderType readerType) {
        String INSERT_READERTYPE="INSERT INTO readertype(id,typename,maxborrownum,limitday) VALUES(?,?,?,?)";
        Object[] args={readerType.getId(),readerType.getTypeName(),readerType.getMaxBorrowNum(),readerType.getLimitDay()};
        int result =jdbcTemplate.update(INSERT_READERTYPE,args);
        if(result==1){
            return 1;
        }
        return 0;
    }

//    根据读者的类型名进行删除读者类型
    public int deleteReaderType(ReaderType readerType) {
        String DELETE_READERTYPE = "DELETE FROM readertype WHERE typename=?";
        Object[] args = {readerType.getTypeName()};
        int result = jdbcTemplate.update(DELETE_READERTYPE,args);
        if(result==1){
            return 1;
        }
        return 0;
    }
//根据读者类型名修改读者类型信息
    public int updateReaderType(ReaderType readerType) {
        String UPDATE_READERTYPE = "update readertype set id=?,typename=?,maxborrownum=?,limitday=? where typename=?";
        Object[] args = {readerType.getId(),readerType.getTypeName(),readerType.getMaxBorrowNum(),readerType.getLimitDay()};
        int result=jdbcTemplate.update(UPDATE_READERTYPE,args);
        if(result==1){
            return 1;
        }
        return 0;
    }
//查询所有的读者类型
    public List<ReaderType> select_All_Reader() {
        String SELECT_BY_TYPE = "select * from readertype";
        RowMapper<ReaderType> readerTypeRowMapper = new BeanPropertyRowMapper<ReaderType>(ReaderType.class);
        readerTypeList = jdbcTemplate.query(SELECT_BY_TYPE,readerTypeRowMapper);
        return readerTypeList;
    }
//根据读者类型名查询读者类型信息
    public ReaderType selectByTypeName(String typeName) {

        String SELECT_BY_TYPE = "select * from readertype where typename=?";
        Object[] arg={typeName};
        RowMapper<ReaderType> readerTypeRowMapper = new BeanPropertyRowMapper<ReaderType>(ReaderType.class);
        readerType = (ReaderType)jdbcTemplate.query(SELECT_BY_TYPE,arg,readerTypeRowMapper);
        return readerType;
    }
}
